package com.emellend.api.animation;

import com.emellend.api.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
/**
 * 随着下拉选的内容不同，字体的移动也不同。
 * @author emellend
 *
 */
public class Animation3 extends Activity implements OnItemSelectedListener {
	
	private static final String[] INTERPOLATORS = {
        "Accelerate", "Decelerate", "Accelerate/Decelerate",
        "Anticipate", "Overshoot", "Anticipate/Overshoot",
        "Bounce"
	};
	private Spinner mSpinner;
	private TextView target;
	private View targetParent;
	private Animation animation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.animation_3);
		init();
	}

	private void init() {
		mSpinner = (Spinner) findViewById(R.id.spinner);
		target = (TextView) findViewById(R.id.target);
		targetParent = (View) target.getParent();
		setAdapter();
		makeAnimation();
	}

	private void setAdapter() {
		ArrayAdapter<String> adapter=
			new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,INTERPOLATORS);
		adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		mSpinner.setAdapter(adapter);
		mSpinner.setOnItemSelectedListener(this);
	}

	private void makeAnimation() {
		float toX=targetParent.getWidth()-target.getWidth()-
				targetParent.getPaddingLeft()-targetParent.getPaddingRight();
		animation = new TranslateAnimation(0.0f, toX, 0.0f, 0.0f);
		animation.setDuration(1000);
		animation.setStartOffset(300);
		animation.setRepeatMode(Animation.RESTART);
		animation.setRepeatCount(Animation.INFINITE);
	}
	
	public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
		switch (position) {
		case 0:
			animation.setInterpolator(this, android.R.anim.accelerate_interpolator);
			break;
		case 1:
			animation.setInterpolator(this, android.R.anim.decelerate_interpolator);
			break;
		case 2:
			animation.setInterpolator(this, android.R.anim.accelerate_decelerate_interpolator);
			break;
		case 3:
			animation.setInterpolator(this, android.R.anim.anticipate_interpolator);
			break;
		case 4:
			animation.setInterpolator(this, android.R.anim.overshoot_interpolator);
			break;
		case 5:
			animation.setInterpolator(this, android.R.anim.anticipate_overshoot_interpolator);
			break;
		case 6:
			animation.setInterpolator(this, android.R.anim.bounce_interpolator);
			break;
		}
		target.startAnimation(animation);
	}
	
	public void onNothingSelected(AdapterView<?> parent) {}
}
